import { Toaster } from 'sonner'
import { useAppQuery } from './hooks/useAppQuery'
import { useAppRouter } from './hooks/useAppRouter'
import { useGetCurrenUserQuery } from './services/user'
import { useAuth } from './hooks/useAuth'
import { Loading } from './components/common/Loading'

function App() {
  const { AppQueryProvider } = useAppQuery()
  return (
    <AppQueryProvider>
      <Toaster position="top-center" toastOptions={{ duration: 3000 }} />
      <InItData />
    </AppQueryProvider>
  )
}

function InItData() {
  const { isLoading, isError, error, data } = useGetCurrenUserQuery()
  const { AppRouterProvider } = useAppRouter()
  const { setUser } = useAuth()
  if (isLoading) return <Loading />
  if (isError) return <div>{JSON.stringify(error)}</div>
  if (data) setUser(data)
  return <AppRouterProvider />
}

export default App
